package com.lmb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmb.bean.Category;
import com.lmb.bean.Dish;
import com.lmb.bean.Setmeal;
import com.lmb.dao.CategoryDao;
import com.lmb.dao.DishDao;
import com.lmb.dao.SetmealDao;
import com.lmb.service.CategoryService;
import com.lmb.service.DishService;
import com.lmb.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @projectName: reggie
 * @package: com.lmb.service.impl
 * @className: CategoryServicelmpl
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/18 8:44
 */
@Service
public class CategoryServicelmpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishDao dishDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public void deleteByIdAssociated(Long ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(ids != null,Dish::getCategoryId,ids);
        Integer dishCount = dishDao.selectCount(dishLambdaQueryWrapper);

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(ids != null,Setmeal::getCategoryId,ids);
        Integer setmealCount = setmealDao.selectCount(setmealLambdaQueryWrapper);
        if (dishCount>0||setmealCount>0){
            throw new ClassCastException("已有关联的菜品或套餐，无法删除");
        }
        this.getBaseMapper().deleteById(ids);
    }
}
